const mongoUtil = require('./mongo_util')
const collectionName = 'users'

/* 用户的增删改查 */
function createUser(user, callback) {
	mongoUtil.getCollection(collectionName).then((collection) => {
		collection.insertOne(user, function (err, res) {
			callback(err, res)
		})
	})
}

function deleteUser(data, callback) {
	mongoUtil.getCollection(collectionName).then((collection) => {
		collection.deleteMany(
			{ username: { $in: data.username } },
			function (err, res) {
				callback(err, res)
			}
		)
	})
}

function updateUser() {}

function getUser(callback) {
	console.log('mongoUtil.getCollection=', mongoUtil.getCollection)
	const mmp = mongoUtil.getCollection(collectionName)
	console.log('mmp=', mmp)

	mmp.then((collection) => {
		collection.find().toArray(function (err, result) {
			// 返回集合中所有数据
			callback(err, result)
		})
	})
}

module.exports = {
	createUser,
	deleteUser,
	updateUser,
	getUser,
}
